import java.awt.Checkbox;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.event.events.ServerMessageEvent;
import com.speljohan.rsbot.event.listeners.PaintListener;
import com.speljohan.rsbot.event.listeners.ServerMessageListener;
import com.speljohan.rsbot.script.Constants;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.wrappers.RSInterface;
import com.speljohan.rsbot.script.wrappers.RSObject;
import com.speljohan.rsbot.script.wrappers.RSTile;

public class TDesGuildMiner2 extends Script implements ActionListener,
		ServerMessageListener, PaintListener {
	// Integers, Doubles, Longs
	public int coalCheckID;
	public int mithrilmineCheckID;
	public int randomint;
	public int randomrunint;
	public int returnint1;
	public int returnint2;
	public int coalMined;
	public int mithMined;
	public int saphirecount;
	public int emeraldcount;
	public int rubycount;
	public int diamondcount;
	public int swingattempts;
	public long runtime;
	public long starttime;
	// Unchanging Integers
	public final int bankID = 11758;
	public final int laddertop = 2113;
	public final int ladderbottom = 30941;
	public final int coalores = 453;
	public final int mithores = 447;
	public final int guilddoorID = 2112;
	// Integer Arrays
	public int[] mithRock = { 31087, 31086, 31088 };
	public int[] coalRock = { 31068, 31069, 31070 };
	// Unchanging Integer Arrays
	public final int[] pickaxeTypes = new int[] { 1275, 1265, 1269, 1267, 1273,
			1271 };
	public final int[] pickaxeHeads = new int[] { 480, 482, 484, 486, 488, 490 };
	public final int[] pickaxeBrokens = new int[] { 468, 470, 472, 474, 476,
			478 };
	public final int[] smokingRocks = { 11433, 11434, 11435, 11436, 11437,
			11438, 11439, 11440, 11441, 11168, 11169, 11170, 11174, 11176,
			11177, 11178, 11179, 11180, 11181, 11182, 31116 };
	// Objects
	public RSObject bankbooth;
	public RSObject topladder;
	public RSObject bottomladder;
	public RSObject coalmine;
	public RSObject mithrilmine;
	public RSObject acoalmine;
	public RSObject amithrilmine;
	// Noteable Locations and Paths
	public RSTile[] bankCheck = { new RSTile(3011, 3355),
			new RSTile(3013, 3355), new RSTile(3015, 3355),
			new RSTile(3014, 3356), new RSTile(3012, 3356),
			new RSTile(3010, 3356) };
	public RSTile topladderl = new RSTile(3021, 3338);
	public RSTile bottomladderl = new RSTile(3021, 9739);
	public RSTile minetile = new RSTile(3029, 9738);
	public RSTile mithriltile = new RSTile(3049, 3736);
	public RSTile mineCenter = new RSTile(3035, 9740);
	public RSTile deathwalktile = new RSTile(3224, 3218);
	RSTile[] mithriltilepath = new RSTile[] { new RSTile(3022, 9739),
			new RSTile(3025, 9739), new RSTile(3028, 9738),
			new RSTile(3031, 9738), new RSTile(3033, 9738),
			new RSTile(3035, 9738), new RSTile(3036, 9738),
			new RSTile(3039, 9740), new RSTile(3042, 9737),
			new RSTile(3043, 9735), new RSTile(3045, 9736),
			new RSTile(3047, 9736) };
	RSTile[] tolowerladder = new RSTile[] { new RSTile(3051, 9740),
			new RSTile(3048, 9741), new RSTile(3046, 9739),
			new RSTile(3043, 9739), new RSTile(3041, 9737),
			new RSTile(3038, 9737), new RSTile(3036, 9738),
			new RSTile(3032, 9738), new RSTile(3030, 9737),
			new RSTile(3026, 9739), new RSTile(3022, 9740) };
	RSTile[] tominecenter = new RSTile[] { new RSTile(3022, 9740),
			new RSTile(3026, 9738), new RSTile(3031, 9738),
			new RSTile(3035, 9737), new RSTile(3039, 9736) };
	RSTile[] tobank = new RSTile[] { new RSTile(3022, 3338),
			new RSTile(3029, 3336), new RSTile(3031, 3340),
			new RSTile(3031, 3346), new RSTile(3029, 3349),
			new RSTile(3028, 3358), new RSTile(3024, 3358),
			new RSTile(3021, 3360), new RSTile(3016, 3360),
			new RSTile(3013, 3356) };
	public RSTile[] deathwalk1 = new RSTile[] { new RSTile(3226, 3218),
			new RSTile(3233, 3220), new RSTile(3233, 3225),
			new RSTile(3231, 3230), new RSTile(3228, 3233),
			new RSTile(3223, 3238), new RSTile(3221, 3242),
			new RSTile(3219, 3247), new RSTile(3218, 3251),
			new RSTile(3217, 3256), new RSTile(3217, 3257),
			new RSTile(3217, 3261), new RSTile(3217, 3266),
			new RSTile(3216, 3272), new RSTile(3216, 3277),
			new RSTile(3210, 3280), new RSTile(3205, 3280),
			new RSTile(3204, 3279), new RSTile(3197, 3279),
			new RSTile(3190, 3282), new RSTile(3184, 3286),
			new RSTile(3178, 3287), new RSTile(3172, 3286),
			new RSTile(3165, 3285), new RSTile(3158, 3291),
			new RSTile(3149, 3296), new RSTile(3143, 3294),
			new RSTile(3137, 3295), new RSTile(3136, 3295),
			new RSTile(3130, 3295), new RSTile(3123, 3299),
			new RSTile(3118, 3296), new RSTile(3112, 3295),
			new RSTile(3106, 3295), new RSTile(3099, 3295),
			new RSTile(3094, 3292), new RSTile(3088, 3290),
			new RSTile(3082, 3289), new RSTile(3076, 3288),
			new RSTile(3074, 3284), new RSTile(3074, 3280),
			new RSTile(3069, 3277), new RSTile(3063, 3277),
			new RSTile(3057, 3276), new RSTile(3050, 3276),
			new RSTile(3047, 3276), new RSTile(3044, 3274),
			new RSTile(3037, 3277), new RSTile(3030, 3278),
			new RSTile(3024, 3276), new RSTile(3018, 3275),
			new RSTile(3013, 3276), new RSTile(3011, 3278),
			new RSTile(3007, 3284), new RSTile(3004, 3289),
			new RSTile(3007, 3294), new RSTile(3006, 3299),
			new RSTile(3004, 3304), new RSTile(3006, 3310),
			new RSTile(3007, 3316), new RSTile(3007, 3321),
			new RSTile(3005, 3326), new RSTile(3007, 3331),
			new RSTile(3007, 3332), new RSTile(3007, 3337),
			new RSTile(3007, 3343), new RSTile(3007, 3349),
			new RSTile(3005, 3355), new RSTile(3007, 3361),
			new RSTile(3012, 3361), new RSTile(3015, 3355) };
	RSTile[] toLaddersTop = new RSTile[] { new RSTile(3012, 3356),
			new RSTile(3013, 3358), new RSTile(3015, 3360),
			new RSTile(3018, 3360), new RSTile(3021, 3358),
			new RSTile(3023, 3357), new RSTile(3024, 3354),
			new RSTile(3024, 3351), new RSTile(3025, 3350),
			new RSTile(3027, 3349), new RSTile(3029, 3347),
			new RSTile(3029, 3345), new RSTile(3029, 3342),
			new RSTile(3029, 3339), new RSTile(3028, 3337),
			new RSTile(3025, 3337), new RSTile(3023, 3338) };
	// State Booleans
	public boolean bankwalking;
	public boolean banking;
	public boolean minewalking;
	public boolean miningcoal;
	public boolean miningmith;
	public boolean minemith;
	public boolean deathwalk;
	public boolean finecontrolling;
	public boolean GUIisDone;
	public boolean coalthread;
	public boolean running;
	public boolean paintgui;
	public boolean fineControlling;

	// END checks to make sure the bot is in the right place at the right time
	// GUI Input Window
	JFrame inputframe;

	JComboBox speedselect;

	JLabel returnSpeed1Label, returnSpeed2Label;

	JTextField customReturnSpeed1, customReturnSpeed2;

	Checkbox paintguibox, minemithbox, doUseFineControl;

	JButton inputvalues;

	JFrame controlframe;

	JButton newSettingsb, stopscriptb, minewalkingb, minecoalb, minemithb,
			bankwalkb, bankb, deathwalkb, continueb;

	// END GUI Input Window
	// Start Event Listeners
	public void actionPerformed(final ActionEvent actEv) {
		if (actEv.getActionCommand().contains("Start")) {
			if (!speedselect.getSelectedItem().equals("Custom")) {
				if (speedselect.getSelectedItem().equals("Medium")) {
					returnint1 = 1000;
					returnint2 = 1250;
					log("Set speed to Medium - Settings ");
				} else if (speedselect.getSelectedItem().equals("Slow")) {
					returnint1 = 2000;
					returnint2 = 3000;
					log("Set speed to Slow - Settings ");
				} else {
					returnint1 = 500;
					returnint2 = 750;
					log("Set speed to Fast - Settings ");
				}
			} else {
				returnint1 = Integer.parseInt(customReturnSpeed1.getText());
				returnint2 = Integer.parseInt(customReturnSpeed2.getText());
				log("Set speed to Custom - Settings ");
			}
			if (paintguibox.getState()) {
				paintgui = true;
				log("Painting GUI - Settings");
			} else {
				paintgui = false;
				log("Not painting GUI - Settings");
			}
			if (minemithbox.getState()) {
				minemith = true;
				log("Mining mithril - Settings");
			} else {
				minemith = false;
				log("Not mining mithril - Settings");
			}
			if (doUseFineControl.getState()) {
				fineControlling = true;
			} else {
				fineControlling = false;
			}
			starttime = System.currentTimeMillis();
			GUIisDone = true;
			log("Closing input window");
			inputframe.dispose();

		} else if (actEv.getActionCommand().contains("Options")) {
			doGUI();
		} else if (actEv.getActionCommand().contains("Stop Script")) {
			controlframe.dispose();
			stopAllScripts();
		} else if (actEv.getActionCommand().contains("Minewalk")) {
			finecontrolling = true;
			setScriptState("minewalkings");
		} else if (actEv.getActionCommand().contains("Mine Coal")) {
			finecontrolling = true;
			setScriptState("miningcoals");
		} else if (actEv.getActionCommand().contains("Mine Mith")) {
			finecontrolling = true;
			setScriptState("miningmiths");
		} else if (actEv.getActionCommand().contains("Bankwalk")) {
			finecontrolling = true;
			setScriptState("bankwalkings");
		} else if (actEv.getActionCommand().equals("Bank")) {
			finecontrolling = true;
			setScriptState("bankings");
		} else if (actEv.getActionCommand().contains("Continue")) {
			finecontrolling = false;
		}
	}

	// END state getting stuff
	// Anti- stuff is here
	public void AntibanMouse() {
		final int intx = random(0, 750);
		final int inty = random(0, 500);
		final int dowemovethemouse = random(1, 5);
		if (dowemovethemouse == 1) {
			wait(random(500, 650));
		} else {
			moveMouse(0, 0, intx, inty);
			log("Moving the mouse");
		}
	}

	public boolean atInventoryItema(final int itemID, final String option) {
		if (getCurrentTab() != Constants.TAB_INVENTORY
				&& !RSInterface.getInterface(Constants.INTERFACE_BANK)
						.isValid()
				&& !RSInterface.getInterface(Constants.INTERFACE_STORE)
						.isValid()) {
			openTab(Constants.TAB_INVENTORY);
		}
		final int[] items = getInventoryArray();
		final java.util.List<Integer> possible = new ArrayList<Integer>();
		for (int i = 0; i < items.length; i++) {
			if (items[i] == itemID) {
				possible.add(i);
			}
		}
		if (possible.size() == 0) {
			return false;
		}
		final int idx = possible.get(random(0, possible.size()));
		final Point t = getInventoryItemPoint(idx);
		if (!option.equals("")) {
			clickMouse(t, 5, 5, false);
			return atMenu(option);
		} else {
			clickMouse(t, 5, 5, true);
		}
		return true;
	}

	// END stuff that involves getting or looking at objects
	// START checks to make sure the bot is in the right place at the right time
	public boolean bankCheckM() {
		for (final RSTile element : bankCheck) {
			if (distanceTo(element) < 5) {
				return true;
			}
		}
		return false;
	}

	public void banknow() {
		if (!getInterface(762).isValid()) {
			if (distanceTo(bankbooth) < 4) {
				log("Clicking the bankbooth");
				lookToTheObject(bankbooth);
				atObject(bankbooth, "Use-quickly");
			} else {
				randomint = random(0, bankCheck.length);
				log("Walking to the bank tile to get closer to the bankbooth");
				walkTileMM(bankCheck[randomint]);
			}
		}
		if (getInterface(762).isValid()) {
			if (getInventoryCountExcept(pickaxeTypes) > 0) {
				log("Depositing everything except pickaxes");
				depositAllExcepta(pickaxeTypes);
			}
			if (getInventoryCountExcept(pickaxeTypes) == 0) {
				log("Closing the bank");
				bank.close();
			}
		}
	}

	public void bankwalknow() {
		if (distanceTo(topladderl) < 50) {
			log("Walking to the bank");
			walkPathMM(tobank, 12);
		}
		if (distanceTo(bottomladderl) < 5) {
			log("Climbing up the ladder");
			lookToTheObject(bottomladder);
			atObject(bottomladder, "Climb-up");
		}
		if (distanceTo(bottomladderl) > 4 && distanceTo(bottomladderl) < 75) {
			log("Walking to the the lower ladder to get up");
			walkPathMM(tolowerladder, 12);
		}
	}

	// END methods that do important stuff
	// Stuff that involves getting our state
	public boolean checkState() {
		if (finecontrolling) {
			return true;
		}
		if (bankCheckM() && getInventoryCountExcept(pickaxeTypes) > 0) {
			setScriptState("bankings");
			return true;
		}
		if (bankCheckM() && getInventoryCountExcept(pickaxeTypes) == 0) {
			setScriptState("minewalkings");
			return true;
		}
		if (distanceTo(topladderl) < 20 && getInventoryCount() < 2) {
			setScriptState("minewalkings");
			return true;
		}
		if (distanceTo(bottomladderl) < 5 && getInventoryCount() < 20) {
			setScriptState("minewalkings");
			return true;
		}
		if (distanceTo(bottomladderl) < 50 && isInventoryFull()) {
			setScriptState("bankwalkings");
			return true;
		}
		if (distanceTo(topladderl) < 45 && !bankCheckM()
				&& getInventoryCount() > 2) {
			setScriptState("bankwalkings");
			return true;
		}
		if (distanceTo(bottomladderl) < 50 && !isInventoryFull()
				&& amithrilmine != null && minemith) {
			setScriptState("miningmiths");
			return true;
		}
		if (distanceTo(bottomladderl) < 50 && !isInventoryFull()
				&& acoalmine != null) {
			setScriptState("miningcoals");
			return true;
		}
		if (distanceTo(deathwalktile) < 30) {
			setScriptState("deathwalkings");
			return true;
		}
		return false;
	}

	// END Event Listeners
	// START modified methods that are made to be more humanlike.
	// depositAllExcepta(int... items) can be found in bank.java
	// atInventoryItema(int itemID, String option) can be found in methods.java
	// remove the a on both of those for the originals, the a ones are only in
	// my script
	public boolean depositAllExcepta(final int... items) {
		int inventoryCount = getInventoryCount();
		int[] inventoryArray = getInventoryArray();
		outer: for (int off = 0; off < inventoryArray.length; off++) {
			if (inventoryArray[off] == -1) {
				continue;
			}
			for (final int item : items) {
				if (inventoryArray[off] == item) {
					continue outer;
				}
			}
			for (int tries = 0; tries < 5; tries++) {
				if (getInventoryCount(inventoryArray[off]) == 1) {
					atInventoryItema(inventoryArray[off], "");
				} else if (getInventoryCount(inventoryArray[off]) <= 5) {
					atInventoryItem(inventoryArray[off], "Deposit-5");
				} else if (getInventoryCount(inventoryArray[off]) <= 10) {
					atInventoryItem(inventoryArray[off], "Deposit-10");
				} else {
					atInventoryItem(inventoryArray[off], "Deposit-All");
				}
				wait(random(1000, 1250));
				if (getInventoryCount() < inventoryCount) {
					break;
				}
			}
			if (getInventoryCount() >= inventoryCount) {
				return false;
			}
			inventoryArray = getInventoryArray();
			inventoryCount = getInventoryCount();
		}
		return true;
	}

	public void doFineControlGUI() {
		controlframe = new JFrame("Fine Control Panel");
		controlframe.setLayout(new GridBagLayout());
		final GridBagConstraints d = new GridBagConstraints();

		d.gridx = 0;
		d.gridy = 0;
		newSettingsb = new JButton("Options");
		controlframe.add(newSettingsb, d);
		newSettingsb.addActionListener(this);

		if (fineControlling) {
			d.gridx = 1;
			d.gridy = 0;
			minewalkingb = new JButton("Minewalk");
			controlframe.add(minewalkingb, d);
			minewalkingb.addActionListener(this);

			d.gridx = 2;
			d.gridy = 0;
			minecoalb = new JButton("Mine Coal");
			controlframe.add(minecoalb, d);
			minecoalb.addActionListener(this);

			d.gridx = 3;
			d.gridy = 0;
			minemithb = new JButton("Mine Mith");
			controlframe.add(minemithb, d);
			minemithb.addActionListener(this);

			d.gridx = 4;
			d.gridy = 0;
			bankwalkb = new JButton("Bankwalk");
			controlframe.add(bankwalkb, d);
			bankwalkb.addActionListener(this);

			d.gridx = 5;
			d.gridy = 0;
			bankb = new JButton("Bank");
			controlframe.add(bankb, d);
			bankb.addActionListener(this);

			d.gridx = 6;
			d.gridy = 0;
			continueb = new JButton("Continue");
			controlframe.add(continueb, d);
			continueb.addActionListener(this);
		}
		d.gridx = 7;
		d.gridy = 0;
		stopscriptb = new JButton("Stop Script");
		controlframe.add(stopscriptb, d);
		stopscriptb.addActionListener(this);

		controlframe.pack();
		controlframe.setVisible(true);
	}

	public void doGUI() {
		final String[] speedValues = { "Medium", "Slow", "Fast", "Custom" };
		inputframe = new JFrame("TheDestroyer's Guildminer Series 2");
		inputframe.setLayout(new GridBagLayout());
		final GridBagConstraints c = new GridBagConstraints();

		c.fill = GridBagConstraints.BOTH;
		c.gridwidth = 2;
		c.gridx = 0;
		c.gridy = 0;
		speedselect = new JComboBox(speedValues);
		inputframe.add(speedselect, c);

		c.gridwidth = 1;
		c.gridx = 0;
		c.gridy = 1;
		returnSpeed1Label = new JLabel("Minimum Speed");
		inputframe.add(returnSpeed1Label, c);

		c.gridx = 1;
		c.gridy = 1;
		customReturnSpeed1 = new JTextField(5);
		inputframe.add(customReturnSpeed1, c);

		c.gridx = 0;
		c.gridy = 2;
		returnSpeed2Label = new JLabel("Maximum Speed");
		inputframe.add(returnSpeed2Label, c);

		c.gridx = 1;
		c.gridy = 2;
		customReturnSpeed2 = new JTextField(5);
		inputframe.add(customReturnSpeed2, c);

		c.gridx = 0;
		c.gridy = 3;
		paintguibox = new Checkbox("Paint GUI");
		inputframe.add(paintguibox, c);

		c.gridx = 1;
		c.gridy = 3;
		minemithbox = new Checkbox("Mine Mithril");
		inputframe.add(minemithbox, c);

		c.gridx = 0;
		c.gridy = 4;
		doUseFineControl = new Checkbox("Fine Control -- Advanced Users Only!");
		inputframe.add(doUseFineControl, c);

		c.gridwidth = 2;
		c.gridx = 0;
		c.gridy = 5;
		inputvalues = new JButton("Confirm Changes/Start Script");
		inputframe.add(inputvalues, c);
		inputvalues.addActionListener(this);

		inputframe.pack();
		inputframe.setVisible(true);
	}

	public String getAuthor() {
		return "TheDestroyer";
	}

	public String getName() {
		return "Guild Miner 2";
	}

	// Methods that are called from the loop that involve getting stuff done
	// (minewalking, bankwalking, banking, and mining. Soon deathwalking
	public boolean getNewRock(final RSObject rockName, final String blahzorz,
			final int checkID, final int[] trueRockIDs) {
		int ontherightrock = 0;
		if (rockName == null) {
			log("The rock was null -" + blahzorz);
			return true;
		}
		if (distanceTo(rockName) > 1 && !getMyPlayer().isMoving()
				&& getMyPlayer().getAnimation() == -1) {
			log("The player is far from the rock and not moving, heading to the rock now! -"
					+ blahzorz);
			return true;
		}
		for (int a = -5; a < 5; a++) {
			if (getAngleToCoordinates(rockName.getLocation().getX(), rockName
					.getLocation().getY()) == getCameraAngle() + a) {
				ontherightrock++;
				log("Camera is on");
			}
		}
		for (int i = 0; i < 2; i++) {
			if (rockName.getID() == trueRockIDs[i]) {
				log("Rock ID is what it should be and the player is doing something so returning false");
				ontherightrock++;
				log("Rock ID is right");
			} else if (i == 2) {
				ontherightrock--;
				ontherightrock--;
				ontherightrock--;
			}
		}
		if (distanceTo(rockName) <= 1) {
			ontherightrock++;
			log("We are close to teh rock");
		} else {
			ontherightrock--;
		}
		if (getMyPlayer().getAnimation() != -1) {
			ontherightrock++;
			log("doing something");
		} else {
			ontherightrock--;
		}
		if (ontherightrock >= 3) {
			log("3 things point to us being on the right rock, not getting a new rock");
			return false;
		}
		log("No state was grabbed, returning true so the bot does something");
		return true;
	}

	// END anti stuff
	// Stuff that involves getting or looking at objects
	public void getObjectsNow() {
		bankbooth = findObject(bankID);
		topladder = findObject(laddertop);
		bottomladder = findObject(ladderbottom);
		acoalmine = findObject(coalRock);
		amithrilmine = findObject(mithRock);
		log("Got the objects we might need to be oriented");
	}

	public void getOffRock() {
		final RSTile location = getMyPlayer().getLocation();
		doWalk(location);
	}

	public String getScriptCategory() {
		return "Mining";
	}

	public String getScriptDescription() {
		String html = "";
		html += "<html>\n";
		html += "<body>\n";
		html += "<h2>" + getName() + " v" + getVersion() + "</h2><br>\n";
		html += "Author: " + getAuthor() + "<br><br>\n";
		html += "Description";
		html += "<center>\n";
		html += "Arguments will be in the GUI\n";
		html += "</center>\n";
		html += "</body>\n";
		html += "</html>\n";
		return html;
	}

	public double getVersion() {
		return 1.04;
	}

	public void lookToTheObject(final RSObject objectname) {
		turnToObject(objectname, random(-5, 5));
		if (objectname == topladder || objectname == bottomladder) {
			setCameraAltitude(true);
		} else if (distanceTo(objectname) >= 7) {
			setCameraAltitude(false);
		} else {
			setCameraAltitude(true);
		}
	}

	public int loop() {
		try {
			randomrunint = random(0, 10);
			AntibanMouse();
			getObjectsNow();
			if (getEnergy() >= 65 + randomrunint) {
				log("Turning on running");
				setRun(true);
			}
			if (getMyPlayer().isInCombat()) {
				runFromCombat();
			}
			if (!checkState()) {
				log("Something went wrong when checking the state, I'm going to shut down and await your restarting");
				// stopAllScripts();
			}
			if (bankwalking) {
				log("Calling the bankwalknow() function");
				bankwalknow();
			}
			if (minewalking) {
				log("Calling the minewalknow() function");
				minewalknow();
			}
			if (miningcoal) {
				log("Calling the getNewRock() function sending information about coalmines");
				if (getNewRock(coalmine, "CoalMine", coalCheckID, coalRock)) {
					log("Calling the minecoal() function");
					minecoal();
					return random(returnint1, returnint2);
				}
				log("Decided to stick with the coal we have");
			}
			if (miningmith) {
				log("Calling the getNewRock() function sending information about mithmines");
				if (getNewRock(mithrilmine, "MithrilMine", mithrilmineCheckID,
						mithRock)) {
					log("Calling the minemith() function");
					minemith();
				}
				log("Decided to stick with the mith we have");
			}
			if (banking) {
				log("Calling the banknow function");
				banknow();
			}
			if (deathwalk) {
				walkPathMM(deathwalk1, 10);
			}
		} catch (final NullPointerException e) {
		}
		return random(returnint1, returnint2);
	}

	public void minecoal() {
		if (coalmine != null) {
			for (final int smokingRock : smokingRocks) {
				if (coalmine.getID() == smokingRock) {
					log("Rock was smoking, jumped off");
					getOffRock();
				}
			}
		}
		coalmine = findObject(coalRock);
		if (coalmine != null) {
			coalCheckID = coalmine.getID();
			log("Found coal mine");
			if (distanceTo(coalmine) > 3) {
				lookToTheObject(coalmine);
				log("Set the camera toward the coal");
			}
			atObject(coalmine, "");
			log("Clicked the coal mine");
		} else {
			log("Didn't find coalmine...");
		}
	}

	public void minemith() {
		if (mithrilmine != null) {
			for (final int smokingRock : smokingRocks) {
				if (mithrilmine.getID() == smokingRock) {
					log("Rock was smoking, jumped off");
					getOffRock();
				}
			}
		}
		mithrilmine = findObject(mithRock);
		if (mithrilmine != null) {
			log("Found mithril mine");
			mithrilmineCheckID = mithrilmine.getID();
			if (distanceTo(mithrilmine) > 5) {
				lookToTheObject(mithrilmine);
			}
			if (distanceTo(mithrilmine) > 5) {
				log("Walking to the mithril tile");
				walkPathMM(mithriltilepath, 12);
			}
			atObject(mithrilmine, "");
			log("Clicked the mith mine");
		} else {
			log("Didn't find mithrilmine...");
		}
	}

	public void minewalknow() {
		if (distanceTo(topladderl) < 5) {
			log("Climbing down the ladder");
			lookToTheObject(topladder);
			for (int i = 0; i < 5; i++) {
				atObject(topladder, "Climb-down");
				if (getMyPlayer().isMoving()) {
					break;
				} else if (distanceTo(topladder) >= 5) {
					break;
				}
				wait(random(5000, 6000));
				if (i == 5) {
					log("We have tried to climb down like, 5 times now, its time to stop. Not working. Screw it.");
					stopAllScripts();
				}
				if (i > 1) {
					log("Climbing down the ladder try number " + i
							+ "failed, rotating camera a bit");
					setCameraRotation(getCameraAngle() + random(40, 55));
				}
			}
		}
		if (distanceTo(topladderl) > 5 && distanceTo(topladderl) < 75) {
			log("Walking to the topside ladders");
			walkPathMM(toLaddersTop, 8);
		}
		if (distanceTo(bottomladderl) < 7) {
			log("Walking to the center of the mine");
			walkPathMM(tominecenter, 12);
		}
	}

	public void onFinish() {
		if (paintgui) {
			Bot.getEventManager().removeListener(PaintListener.class, this);
		}
		running = false;
	}

	public void onRepaint(final Graphics g) {
		long runtime = System.currentTimeMillis() - starttime;
		final long hours = runtime / (1000 * 60 * 60);
		runtime -= hours * 1000 * 60 * 60;
		final long minutes = runtime / (1000 * 60);
		runtime -= minutes * 1000 * 60;
		if (isLoggedIn()) {
			g.setColor(Color.red);
			g
					.drawString("Coal mined: " + Integer.toString(coalMined),
							220, 365);
			g.drawString("Mithril mined: " + Integer.toString(mithMined), 220,
					380);
			g.drawString("Saphires found: " + Integer.toString(saphirecount),
					220, 395);
			g.drawString("Emeralds found: " + Integer.toString(emeraldcount),
					220, 410);
			g.drawString("Rubies found: " + Integer.toString(rubycount), 220,
					425);
			g.drawString("Diamonds found: " + Integer.toString(diamondcount),
					220, 440);
			g.drawString("Time Run: " + Long.toString(hours) + " Hours "
					+ Long.toString(minutes) + " Minutes", 220, 455);
		}
	}

	public boolean onStart(final String[] args) {
		doGUI();
		while (!GUIisDone) {
			wait(random(400, 600));
		}
		doFineControlGUI();
		running = true;
		return true;
	}

	public void runFromCombat() {
		RSTile runTile = new RSTile(getMyPlayer().getLocation().getX()
				+ random(5, 7), getMyPlayer().getLocation().getY()
				+ random(5, 7));
		RSObject isTileSolid = getObjectAt(runTile);
		if (isTileSolid == null) {
			walkTileMM(runTile);
		} else {
			runTile = new RSTile(getMyPlayer().getLocation().getX()
					- random(5, 7), getMyPlayer().getLocation().getY()
					- random(5, 7));
			isTileSolid = getObjectAt(runTile);
			if (isTileSolid == null) {
				walkTileMM(runTile);
			} else {
				log("Gah, we must be in a tight situation... I can't do anything, sorry. Odds are you're going to die...");
			}
		}
	}

	public void serverMessageRecieved(final ServerMessageEvent e) {
		final String message = e.getMessage();
		if (!super.isActive) {
			Bot.getEventManager().removeListener(ServerMessageListener.class,
					this);
			return;
		}
		if (message.indexOf("manage to mine some coal") != -1) {
			coalMined++;
		}
		if (message.indexOf("manage to mine some mithril") != -1) {
			mithMined++;
		}
		if (message.indexOf("just found a Sapphire!") != -1) {
			saphirecount++;
		}
		if (message.indexOf("just found a Emerald!") != -1) {
			emeraldcount++;
		}
		if (message.indexOf("just found a Ruby!") != -1) {
			rubycount++;
		}
		if (message.indexOf("just found a Diamond!") != -1) {
			diamondcount++;
		}
	}

	public void setScriptState(final String varName) {
		bankwalking = false;
		banking = false;
		minewalking = false;
		miningcoal = false;
		miningmith = false;
		deathwalk = false;
		if (varName.contains("bankwalkings")) {
			bankwalking = true;
		} else if (varName.contains("bankings")) {
			banking = true;
		} else if (varName.contains("minewalkings")) {
			minewalking = true;
		} else if (varName.contains("miningcoals")) {
			miningcoal = true;
		} else if (varName.contains("miningmiths")) {
			miningmith = true;
		} else if (varName.contains("deathwalkings")) {
			deathwalk = false;
		}
	}
}